package com.lantosec.hmrp.modules.cms.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.lantosec.hmrp.core.config.InitDataListener;
import com.lantosec.hmrp.core.shiro.TokenUtil;
import com.lantosec.hmrp.core.util.DateUtils;
import com.lantosec.hmrp.modules.cms.entity.CmsArticle;
import com.lantosec.hmrp.modules.cms.entity.CmsAttachment;
import com.lantosec.hmrp.modules.cms.mapper.CmsArticleMapper;
import com.lantosec.hmrp.modules.cms.service.ICmsArticleService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lantosec.hmrp.modules.cms.service.ICmsAttachmentService;
import com.lantosec.hmrp.modules.cms.vo.CmsArticleVO;
import com.lantosec.hmrp.modules.cms.vo.CmsAttachmentVO;
import com.lantosec.hmrp.modules.uploads.entity.SysUploads;
import com.lantosec.hmrp.modules.uploads.service.ISysUploadsService;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.context.annotation.Primary;
import lombok.extern.log4j.Log4j2;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

/**
 * <p>
 * CMS文章列表 服务实现类
 * </p>
 *
 * @author AutoCode
 * @since 2020-07-06
 */
@Service
@Primary
@Log4j2
public class CmsArticleServiceImpl extends ServiceImpl<CmsArticleMapper, CmsArticle> implements ICmsArticleService {

    @Autowired
    private ICmsAttachmentService iCmsAttachmentService;

    @Autowired
    private ISysUploadsService iSysUploadsService;

    @Override
    public List<CmsArticleVO> getCmsArticle(String categoryId,String content) {
        List<CmsArticleVO> cmsArticleVOS = baseMapper.getCmsArticles(categoryId,content);
        cmsArticleVOS.forEach(param -> {
            //查询文章附件列表
            List<CmsAttachmentVO> cmsAttachmentVOS = getCmsAttachmentVOS(param.convertToEntity());
            param.setCmsAttachmentVOS(cmsAttachmentVOS);
            //返回文章缩略图
            String thumbnail = param.getThumbnail();
            if(StringUtils.isNotEmpty(thumbnail)){
                SysUploads sysUploads = iSysUploadsService.getById(thumbnail);
                String savePath = InitDataListener.uploadsUrl+sysUploads.getSavePath()+sysUploads.getFileName();
                param.setSavePath(savePath);
            }
        });
        return cmsArticleVOS;
    }

    /**
     * 查询文章附件列表
     */
    private List<CmsAttachmentVO> getCmsAttachmentVOS(CmsArticle param) {
        List<CmsAttachment> cmsAttachments = iCmsAttachmentService.list(new QueryWrapper<CmsAttachment>().eq(CmsAttachment.ColName.article_id.toString(), param.getArticleId()));
        List<CmsAttachmentVO> cmsAttachmentVOS = new ArrayList<>();
        cmsAttachments.forEach(cmsAttachment->{
            CmsAttachmentVO cmsAttachmentVO = cmsAttachment.convertToVO();
            String uploadId = cmsAttachment.getUploadId();
            SysUploads sysUploads = iSysUploadsService.getById(uploadId);
            String savePath = InitDataListener.uploadsUrl+sysUploads.getSavePath()+sysUploads.getFileName();
            cmsAttachmentVO.setSavePath(savePath);
            cmsAttachmentVOS.add(cmsAttachmentVO);
        });
        return cmsAttachmentVOS;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void saveCmsArticle(CmsArticleVO vo) {
        if(Objects.nonNull(vo)){
            CmsArticle entity = vo.convertToEntity();
            String issuer = entity.getIssuer();
            if(StringUtils.isEmpty(issuer)){
                String userName = TokenUtil.getName();
                entity.setIssuer(userName);
            }
            entity.setEditorId(TokenUtil.getUserId());
            entity.setEditorName(TokenUtil.getName());
            entity.setEditorTime(DateUtils.nowDate());
            if(null == entity.getArticleFlag()){
                entity.setArticleFlag(4);
            }
            this.save(entity);
            //新增文章附件
            List<CmsAttachmentVO> cmsAttachmentVOS = vo.getCmsAttachmentVOS();
            if(CollectionUtils.isNotEmpty(cmsAttachmentVOS)){
                cmsAttachmentVOS.forEach(param->{
                    param.setArticleId(entity.getArticleId());
                    param.setEditorId(TokenUtil.getUserId());
                    param.setEditorName(TokenUtil.getName());
                    param.setEditorTime(DateUtils.nowDate());
                    iCmsAttachmentService.save(param.convertToEntity());
                });
            }
        }
    }
}
